package com.cloud.school.bus.teacherhelper.modules.home;

import java.nio.channels.SelectableChannel;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ViewFlipper;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.utils.ScreenUtils;
import com.android.support.jhf.utils.ToolUtils;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.activity.BaseActivity;
import com.cloud.school.bus.teacherhelper.base.anim.swap3d.Constants;
import com.cloud.school.bus.teacherhelper.base.anim.swap3d.RotationHelper;
import com.cloud.school.bus.teacherhelper.base.dialog.CustomAlertDialog;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
import com.cloud.school.bus.teacherhelper.entitys.Picture;
import com.cloud.school.bus.teacherhelper.entitys.Student;
import com.cloud.school.bus.teacherhelper.modules.home.adapter.PictureThumbnailsAdapter;
import com.cloud.school.bus.teacherhelper.modules.home.view.EditContentView;
import com.cloud.school.bus.teacherhelper.modules.home.view.SelectedStuView;
import com.cloud.school.bus.teacherhelper.modules.notice.adapter.StuSelectAdapter;

/**
 * 谁在照片里，发送文件到学生
 * flag 0表示所有照片，1表示相机拍摄 
 * 
 * @author lenovo
 * 
 */
public class SendFileToStuActivity extends BaseActivity{

	public static final int SELECTED_STU = 0;
	public static final int EDIT_CONTENT = 1;

	private List<Picture> mPictures;
	private ViewFlipper mViewFlipper;
	private SelectedStuView mSelectedStuView;
	private EditContentView mEditContentView;
	/**0表示所有照片，1表示相机拍摄 */
	private int mFlag = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_send_file_to_stu);

		init();
	}

	@Override
	protected void init() {

		mPictures = (List<Picture>) getIntent().getSerializableExtra(
				"pictureList");
		mFlag = getIntent().getIntExtra("flag", 0);
		
		mApplication.imageLoaderInit(400, 400);

		mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);

		mSelectedStuView = new SelectedStuView(mContext, mPictures,
				mViewFlipper, mFlag);
		mEditContentView = new EditContentView(mContext, mViewFlipper);
		mSelectedStuView.setEditContentView(mEditContentView);
		mEditContentView.setSelectedStuView(mSelectedStuView);
		
		mViewFlipper.addView(mSelectedStuView, SELECTED_STU);
		mViewFlipper.addView(mEditContentView, EDIT_CONTENT);

		setListener();
	}

	private void cancel() {
		if (mViewFlipper.getDisplayedChild() == EDIT_CONTENT) {
			mEditContentView.getRotationHelper().applyRotation(Constants.viewA, 0, -90);
		} else {
			CustomAlertDialog customAlertDialog = new CustomAlertDialog(
					mContext, R.style.CustomAlertDialog);
			customAlertDialog.setTitleMessage("提示", Color.BLACK);
			customAlertDialog.setMessage("取消发送照片");
			customAlertDialog.setLeftButton("是", new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					mApplication.imageLoaderInit(80, 80);
					finish();
				}
			});
			customAlertDialog.setRightButton("否", new OnClickListener() {

				@Override
				public void onClick(View arg0) {

				}
			});
			customAlertDialog.show();
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(null != mSelectedStuView){
			mSelectedStuView.onActivityResult(requestCode, resultCode, data);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			cancel();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
